package org.ligson.hbase127.fw;

import org.apache.commons.lang.reflect.FieldUtils;
import org.springframework.core.annotation.AnnotationUtils;

public class DefaultRowIdGenerator implements RowIdGenerator {
    @Override
    public String generator(Object e) {
        Table table = AnnotationUtils.findAnnotation(e.getClass(), Table.class);
        if (table == null) {
            throw new RuntimeException(String.format("实体:%s没有Table注解", e.getClass().getSimpleName()));
        }
        Object id;
        try {
            id = FieldUtils.readDeclaredField(e, table.idName());
        } catch (IllegalAccessException illegalAccessException) {
            throw new RuntimeException(String.format("实体：%s不存在ID字段：%s", e.getClass().getSimpleName(), table.idName()));
        }
        String tableName = table.value();
        return "row-key-" + tableName + "-" + id;
    }
//    @Override
//    public String generator(E e) {
//        Table table = AnnotationUtils.findAnnotation(e.getClass(), Table.class);
//        String tableName = table.value();
//        PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(e.getClass());
//        for (PropertyDescriptor pd : pds) {
//            Field field = FieldUtils.getDeclaredField(e.getClass(), pd.getName());
//            ID id = field.getDeclaredAnnotation(ID.class);
//            if(){
//
//            }
//        }
//        return null;
//    }
}
